// pages/authentication1/authentication1.js
const utils = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: "",
        idCard: '',
        dateTime: '',
        images1: '',
        images2: '',
        ocrId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        if (wx.getStorageSync('info')) {
            var info = wx.getStorageSync('info');
            this.setData({
                name: info.name,
                idCard: info.idCardNumber,
                dateTime: info.validDate,
                images1: info.frontUrl,
                images2: info.backUrl,
                ocrId: info.id
            })
        }
    },
    formName: function (e) {
        this.setData({
            name: e.detail.value.replace(new RegExp(" ", "gm"), "")
        })
    },

    submit: function () {
        let that = this;
        if (that.data.name == "") {
            wx.showToast({
                title: '请输入姓名',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        wx.showLoading({
            title: '请稍等！'
        });
        utils.FetchRequest('/v1/api/ocrIdcardSave',
            {
                realName: that.data.name,
                ocrId: that.data.ocrId
            }).then((res) => {
            if (res.data.status == 200) {
                wx.showToast({
                    title: '实名认证成功',
                    icon: 'none',
                    duration: 2000
                });
                utils.FetchRequest('/v1/api/userInfo', {}, "get"
                ).then((res) => {
                    wx.setStorageSync('info', res.data.data);
                    wx.getStorage({
                        key: 'info', success: (res) => {
                            console.log(res.data, '用户基本信息')
                        }
                    })
                });
                setTimeout(()=>{
                    wx.switchTab({
                        url: "/pages/index/index"
                    })
                },2000);
                wx.hideLoading();
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        })
    },

    preview: function (e) {
        let currentUrl = e.currentTarget.dataset.src;
        wx.previewImage({
            current: currentUrl, // 当前显示图片的http链接
            urls: [currentUrl]
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.hideHomeButton()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    }
})